﻿Public Class DrogueriaDAO

    Public Shared Function insertarDrogueria(ByVal DrogueriaCodigo As Integer, ByVal DrogueriaNombre As String) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Drogueria"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@DrogueriaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@DrogueriaCodigo", DrogueriaCodigo).DbType = DbType.Int32
                    .AddWithValue("@DrogueriaNombre", DrogueriaNombre).DbType = DbType.String
                End With
                .Connection = cn
                Return .ExecuteScalar
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDroguerias(ByVal DrogueriaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_Drogueria_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@DrogueriaId", DrogueriaId).DbType = DbType.Int32
            da.Fill(ds, "Droguerias")

            Return ds.Tables("Droguerias")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDrogueriaId(ByVal DrogueriaCodigo As Integer) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand
            cmd.Connection = cn
            cmd.CommandText = "Select DrogueriaId From Drogueria Where DrogueriaCodigo = " & DrogueriaCodigo.ToString
            Dim DrogueriaId As Integer = cmd.ExecuteScalar

            Return DrogueriaId
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarDrogueria(ByVal DrogueriaId As Integer, ByVal DrogueriaCodigo As Integer, ByVal DrogueriaNombre As String) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Drogueria"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@DrogueriaId", DrogueriaId).DbType = DbType.Int32
                    .AddWithValue("@DrogueriaNombre", DrogueriaNombre).DbType = DbType.String
                    .AddWithValue("@DrogueriaCodigo", DrogueriaCodigo).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarDrogueria(ByVal DrogueriaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Drogueria"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@DrogueriaId", DrogueriaId).DbType = DbType.Int32
                    .AddWithValue("@DrogueriaNombre", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@DrogueriaCodigo", Constante.VACIO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function


End Class
